package com.yanlight.app.request;


import com.yanlight.app.response.WxCertResponse;
import com.yanlight.support.WxConstants;
import com.yanlight.support.http.AbstractRequest;
import com.yanlight.support.http.RequestBuilder;

import java.util.SortedMap;
import java.util.TreeMap;

public class WxCertRequest extends AbstractRequest<WxCertResponse> {

    private String tradeUrl = WxConstants.WX_MICRO_CERT;

    private String mch_id;

    private String nonce_str;

    private String sign;

    private String sign_type = "HMAC-SHA256";

    private SortedMap<String, String> paramsMap;

    public String getMch_id() {
        return mch_id;
    }

    public void setMch_id(String mch_id) {
        this.mch_id = mch_id;
    }

    public String getNonce_str() {
        return nonce_str;
    }

    public void setNonce_str(String nonce_str) {
        this.nonce_str = nonce_str;
    }

    public String getSign() {
        return sign;
    }

    public void setSign(String sign) {
        this.sign = sign;
    }

    public String getSign_type() {
        return sign_type;
    }

    public void setSign_type(String sign_type) {
        this.sign_type = sign_type;
    }

    public SortedMap<String, String> signParams() {
        SortedMap<String, String> signParams = new TreeMap<String, String>();
        signParams.put("mch_id", getMch_id());
        signParams.put("nonce_str", getNonce_str());
        signParams.put("sign_type", getSign_type());
        return signParams;
    }

    public SortedMap<String, String> requestParams() {
        if (paramsMap == null) {
            paramsMap = new TreeMap<String, String>();
        }
        paramsMap.put("mch_id", mch_id);
        paramsMap.put("nonce_str", nonce_str);
        paramsMap.put("sign", sign);
        paramsMap.put("sign_type", sign_type);
        return paramsMap;
    }

    @Override
    protected void execute(RequestBuilder builder) {
        builder.addParams(requestParams());
        builder.post().contentXML().dataXML().setUrl(tradeUrl);
    }
}
